<script>
import {getDictDataAPI} from "@/apis/comm/dictData";
import {getAllDeptAPI} from "@/apis/system/dept";
import {collectFee, doInvalid, doReturn, queryRegistrationForPage} from "@/apis/docters/registration";

export default {
  name: "RegisteredList",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 挂号数据
      regTableList: [],
      // 部门数据
      deptList: [],
      // 用户数据
      userList: [],
      // 挂号单状态数据字典
      regStatusOptions: [],
      // 门诊急诊
      schedulingTypeOptions: [],
      // 上午  中午  晚上
      subsectionTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: undefined,
        deptId: undefined,
        schedulingType: undefined,
        subsectionType: undefined,
        visitDate: undefined,
        queryDate: new Date()
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      // 加载查询条件排班类型1 门诊 2 急诊 字典表数据翻译
      getDictDataAPI('his_scheduling_type').then(res => {
        this.schedulingTypeOptions = res.data
      })
      // 加载排班时段1上午  2下午 3晚上 字典表数据翻译
      getDictDataAPI('his_subsection_type').then(res => {
        this.subsectionTypeOptions = res.data
      })
      // 加载挂号状态0未收费,1待就诊，2,就诊中，3，就诊完成，4，已退号，5 作废
      getDictDataAPI('his_registration_status').then(res => {
        this.regStatusOptions = res.data
      })
      getAllDeptAPI().then(res => {
        this.deptList = res.data
      })

      // selectNeedSchedulingUsers().then(res => {
      //   this.userList = res.data
      // })
      // 查询挂号信息
      this.listRegistration()
    },
    // 搜索
    handleQuery() {
      this.listRegistration()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.listRegistration()
    },
    listRegistration() {
      this.loading = true
      this.queryParams.visitDate = this.moment(this.queryParams.queryDate).format('YYYY-MM-DD')
      queryRegistrationForPage(this.queryParams).then(res => {
        this.regTableList = res.data.records
        this.total = res.data.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 每页显示多少条的数据变化
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.listRegistration()
    },
    // 分页跳转
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.listRegistration()
    },
    // 翻译部门
    deptIdFormatter(row, column) {
      let deptName
      this.deptList.filter(dept => {
        if (dept.deptId === row.deptId) {
          deptName = dept.deptName
        }
      })
      return deptName
    },
    // 翻译排班类型
    schedulingTypeFormatter(row, column) {
      return this.selectDictLabel(this.schedulingTypeOptions, row.schedulingType)
    },
    // 翻译排班时段
    subsectionTypeFormatter(row, column) {
      return this.selectDictLabel(this.subsectionTypeOptions, row.subsectionType)
    },
    // 翻译挂号单类型
    regStatusFormatter(row, column) {
      return this.selectDictLabel(this.regStatusOptions, row.registrationStatus)
    },

    // 收费
    handleCollect(row) {
      this.loading = true
      collectFee(row.registrationId).then(res => {
        this.loading = false
        this.msgSuccess('收费成功')
        this.listRegistration()
      }).catch(() => {
        this.msgError('收费失败')
      })
    },
    // 作废
    handleInvalid(row) {
      this.loading = true
      doInvalid(row.registrationId).then(res => {
        this.loading = false
        this.msgSuccess('作废成功')
        this.listRegistration()
      }).catch(() => {
        this.msgError('作废失败')
      })
    },
    // 退号
    handleReturn(row) {
      this.loading = true
      doReturn(row.registrationId).then(res => {
        this.loading = false
        this.msgSuccess('退号成功')
        this.listRegistration()
      }).catch(() => {
        this.msgError('退号失败')
      })
    }
  }
}
</script>

<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="所属部门" prop="deptId">
        <el-select
            v-model="queryParams.deptId"
            placeholder="请选择所属部门"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="d in deptList"
              :key="d.deptId"
              :label="d.deptName"
              :value="d.deptId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="患者名称" prop="patientName">
        <el-input
            v-model="queryParams.patientName"
            placeholder="请输入患者名称"
            clearable
            size="small"
            style="width:200px"/>
      </el-form-item>
      <el-form-item label="挂号类型" prop="schedulingType">
        <el-select
            v-model="queryParams.schedulingType"
            placeholder="可用状态"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in schedulingTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号时段" prop="subsectionType">
        <el-select
            v-model="queryParams.subsectionType"
            placeholder="挂号时段"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in subsectionTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号状态" prop="regStatus">
        <el-select
            v-model="queryParams.regStatus"
            placeholder="挂号状态"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in regStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号时间" prop="queryDate">
        <el-date-picker
            v-model="queryParams.queryDate"
            size="small"
            style="width:200px"
            value-format="yyyy-MM-dd"
            type="date"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="normal" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <!-- 数据表格开始 -->
    <el-table v-loading="loading" border :data="regTableList">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="right" inline class="demo-table-expand">
            <el-form-item label="挂号ID:">
              <span>{{ props.row.registrationId }}</span>
            </el-form-item>
            <el-form-item label="挂号员:">
              <span>{{ props.row.createBy }}</span>
            </el-form-item>
            <el-form-item label="创建时间:">
              <span>{{ props.row.createTime }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="patientName"/>
      <el-table-column label="挂号科室" align="center" prop="deptId" :formatter="deptIdFormatter"/>
      <el-table-column label="接诊医生" align="center" prop="doctorName"/>
      <el-table-column label="挂号费用" align="center" prop="registrationAmount">
        <template slot-scope="scope">
          <span>{{ scope.row.registrationAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流水编号" align="center" prop="registrationNumber"/>
      <el-table-column label="状态" align="center" prop="registrationStatus" :formatter="regStatusFormatter"/>
      <el-table-column label="就诊日期" align="center" prop="visitDate"/>
      <el-table-column label="挂号类型" align="center" prop="schedulingType" :formatter="schedulingTypeFormatter"/>
      <el-table-column label="挂号时段" align="center" prop="subsectionType" :formatter="subsectionTypeFormatter"/>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button v-if="scope.row.registrationStatus=='0'" type="success" icon="el-icon-check" size="mini"
                     @click="handleCollect(scope.row)">收费
          </el-button>
          <el-button v-if="scope.row.registrationStatus=='1'" type="danger" icon="el-icon-close" size="mini"
                     @click="handleReturn(scope.row)">退号
          </el-button>
          <el-button v-if="scope.row.registrationStatus=='0'" type="danger" icon="el-icon-check" size="mini"
                     @click="handleInvalid(scope.row)">作废
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
  </div>
</template>

<style scoped>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
